package com.asion.vmovie.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.asion.vmovie.R;
import com.asion.vmovie.activities.WebActivity;
import com.asion.vmovie.adapters.BaseRecyclerAdapter;
import com.asion.vmovie.beans.DetialsBean;
import com.asion.vmovie.ui.MVPContract.MuHouContract;
import com.asion.vmovie.ui.MVPModel.MuHouModel;
import com.asion.vmovie.ui.MVPPresenter.MuHouDetailPresenter;
import com.asion.vmovie.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

/**
 * Created by asion on 16-10-18.
 */

public class BaseRecyclerFragment extends BaseFragment<MuHouDetailPresenter, MuHouModel> implements MuHouContract.DetialView {
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.refreshlayout)
    SwipeRefreshLayout refreshlayout;
    private BaseRecyclerAdapter baseRecyclerAdapter;
    private String cateid;

    public static BaseRecyclerFragment newInstance(String cateid) {
        Bundle bundle = new Bundle();
        bundle.putString("cateid", cateid);
        BaseRecyclerFragment baseRecyclerFragment = new BaseRecyclerFragment();
        baseRecyclerFragment.setArguments(bundle);
        return baseRecyclerFragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.recycler_fragment;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
        mPresenter.getDetialData(1, 10, Integer.parseInt(cateid));
    }

    @Override
    protected void initView() {
        refreshlayout.setColorSchemeColors(Color.BLUE,Color.CYAN,Color.GRAY);
        Bundle arguments = getArguments();
        cateid = arguments.getString("cateid");

        baseRecyclerAdapter = new BaseRecyclerAdapter(getContext());
        recycler.setItemAnimator(new DefaultItemAnimator());
        recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        recycler.setAdapter(baseRecyclerAdapter);
        refreshlayout.setOnRefreshListener(() -> {
            page=0;
            baseRecyclerAdapter.removeAll();
            new Handler().postDelayed(() -> refreshlayout.setRefreshing(false), 2000);
        });

        baseRecyclerAdapter.setOnItemClickListener(dataBean -> {
            Bundle bundle = new Bundle();
            bundle.putString("postId", dataBean.getPostid());
            bundle.putString("url", dataBean.getRequest_url());
            bundle.putString("likecount", dataBean.getLike_num());
            bundle.putString("sharecount", dataBean.getShare_num());
            bundle.putString("commentcount", dataBean.getDuration());
            WebActivity.showAction(getActivity(), bundle);
        });

        if(cateid != null){
            recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    if(dy > 0){
                        EventBus.getDefault().post("hide");
                    }else{
                        EventBus.getDefault().post("show");
                    }
                    LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    int itemCount = layoutManager.getItemCount();
                    int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                    if (!loading && itemCount < (lastVisibleItemPosition + 2)) {
                        mPresenter.getDetialData(++page, 10, Integer.parseInt(cateid));
                        loading = true;
                    }
                }
            });
        }
    }

    private boolean loading = false;
    private int page = 1;

    @Override
    public void showDataSuccess(DetialsBean detialsBean) {
        baseRecyclerAdapter.addAll(detialsBean.getData());
        loading = false;
    }

    @Override
    public void showDataError(Throwable throwable) {
        ToastUtil.showShort("网络请求失败...");
    }
}
